package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ConfigHouseBlockMapper;
import com.ruoyi.system.domain.ConfigHouseBlock;
import com.ruoyi.system.service.IConfigHouseBlockService;

/**
 * 商业区管理Service业务层处理
 * 
 * @author YuKunXu
 * @date 2024-02-27
 */
@Service
public class ConfigHouseBlockServiceImpl implements IConfigHouseBlockService 
{
    @Autowired
    private ConfigHouseBlockMapper configHouseBlockMapper;

    /**
     * 查询商业区管理
     * 
     * @param id 商业区管理主键
     * @return 商业区管理
     */
    @Override
    public ConfigHouseBlock selectConfigHouseBlockById(Long id)
    {
        return configHouseBlockMapper.selectConfigHouseBlockById(id);
    }

    /**
     * 查询商业区管理列表
     * 
     * @param configHouseBlock 商业区管理
     * @return 商业区管理
     */
    @Override
    public List<ConfigHouseBlock> selectConfigHouseBlockList(ConfigHouseBlock configHouseBlock)
    {
        return configHouseBlockMapper.selectConfigHouseBlockList(configHouseBlock);
    }

    /**
     * 新增商业区管理
     * 
     * @param configHouseBlock 商业区管理
     * @return 结果
     */
    @Override
    public int insertConfigHouseBlock(ConfigHouseBlock configHouseBlock)
    {
        String username = SecurityUtils.getUsername();
        configHouseBlock.setCreatorUserId(username);
        return configHouseBlockMapper.insertConfigHouseBlock(configHouseBlock);
    }

    /**
     * 修改商业区管理
     * 
     * @param configHouseBlock 商业区管理
     * @return 结果
     */
    @Override
    public int updateConfigHouseBlock(ConfigHouseBlock configHouseBlock)
    {
        String username = SecurityUtils.getUsername();
        configHouseBlock.setLastModifyUserId(username);
        return configHouseBlockMapper.updateConfigHouseBlock(configHouseBlock);
    }

    /**
     * 批量删除商业区管理
     * 
     * @param ids 需要删除的商业区管理主键
     * @return 结果
     */
    @Override
    public int deleteConfigHouseBlockByIds(Long[] ids)
    {
        for (long id : ids){
            int num = hasBlock(id);
            if (num > 0){
                return -1;
            } else {
                ConfigHouseBlock configHouseBlock = new ConfigHouseBlock();
                configHouseBlock.setDeleteUserId(String.valueOf(SecurityUtils.getUserId()));
                configHouseBlock.setId(id);
                return configHouseBlockMapper.deleteConfigHouseBlockByIds(configHouseBlock);
            }
        }
        return 0;
    }



    /**
     * 判断商业区下面是否有楼栋， 有楼栋则不可删除
     */
    public int hasBlock(long id) {
        int num = configHouseBlockMapper.selectConfigBuilding(id);
        return num;
    }


    /**
     * 删除商业区管理信息
     * 
     * @param id 商业区管理主键
     * @return 结果
     */
    @Override
    public int deleteConfigHouseBlockById(Long id)
    {
        return configHouseBlockMapper.deleteConfigHouseBlockById(id);
    }

    @Override
    public List<ConfigHouseBlock> selectTreeList() {
        return configHouseBlockMapper.selectTreeList();
    }
}
